<?php
declare (strict_types = 1);

namespace app\fund\controller;

use app\BaseController;
use app\fund\model\FundModel;
use app\Request;
use think\facade\View;

class Index extends BaseController
{
    /**
     * 基金列表
     *
     * @Author tanruizheng
     * @DateTime 2021-09-02
     * @param Request $request
     * @return void
     */
    public function index(Request $request){
        if($request->param('_action') == 'getList'){
            $where = [];
            //列表
            $list = FundModel::where($where)->select()->toArray();
            foreach ($list as &$item) {
                //计算预计收益
                $increase = $item['increase'] / 100;
                $income = $item['share_held'] * $increase * $item['average_nav'];
                $item['income'] = floatval(sprintf('%.2f', $income));
                $item['increase'] = $item['increase'] >0 ? '+'.$item['increase'] : $item['increase'];
                $item['income'] = $item['income'] >0 ? '+'.$item['income'] : $item['income'];
            }
            //总数
            $count = FundModel::where($where)->count();
            return json(self::createList($list, $count));
        }
        return View::fetch('index',[
           
        ]);
    }

    /**
     * 修改单个数据
     *
     * @Author tanruizheng
     * @DateTime 2021-09-02
     * @return void
     */
    public function editSingle(Request $request){
        $id = $request->param('id');
        $field = $request->param('field');
        $value = $request->param('value');
        if(empty($id)){
            return self::createFail('ID不能为空');
        }
        if(empty($field)){
            return self::createFail('field不能为空');
        }
        $res = FundModel::where('id', $id)->save([
            $field => $value
        ]);
        if($res !== false){
            return self::createSuccess('编辑成功');
        }
        return self::createFail('编辑失败');
    }

    /**
     * 添加
     *
     * @Author tanruizheng
     * @DateTime 2021-07-13
     * @param Request $request
     * @return void
     */
    public function add(Request $request){
        if($request->isPost()){
            $params = $request->param();
            $data = [
                'title' => $params['title'],
                'share_held' => $params['share_held'],
                'average_nav' => $params['average_nav'],
                'increase' => $params['increase'],
            ];
            $res = FundModel::create($data);
            if($res !== false){
                return self::createSuccess('添加成功');
            }
            return self::createFail('添加失败');
        }
        return View::fetch('add',[
           
        ]);
    }

    /**
     * 编辑
     *
     * @Author tanruizheng
     * @DateTime 2021-07-13
     * @param Request $request
     * @return void
     */
    public function edit(Request $request){
        if($request->isPost()){
            $params = $request->param();
            if(empty($params['id'])){
                return self::createFail('ID不能为空');
            }
            $data = [
                'title' => $params['title'],
                'share_held' => $params['share_held'],
                'average_nav' => $params['average_nav'],
                'increase' => $params['increase'],
            ];
            $res = FundModel::where('id', $params['id'])->save($data);
            if($res !== false){
                return self::createSuccess('编辑成功');
            }
            return self::createFail('编辑失败');
        }
        $id = $request->param('id',0);
        $data = FundModel::where('id',$id)->find()->toArray();
        return View::fetch('edit',[
            'data' => $data
        ]);
    }

    /**
     * 删除
     *
     * @Author tanruizheng
     * @DateTime 2021-07-13
     * @param Request $request
     * @return void
     */
    public function delete(Request $request){
        $id = $request->param('id',0);
        if(!$id){
            return self::createFail('参数错误');
        }
        $res = FundModel::where('id', $id)->delete();
        if($res !== false){
            return self::createSuccess('删除成功');
        }
        return self::createFail('删除失败');
    }
}
